﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NDjango.Compatibility;
using NDjango.Interfaces;
using System.Globalization;
using MetaCollection.Framework.Model;

namespace MetaCollection.ContentService.Template
{
	[CLSCompliant(false)]
	public class ZoneItemTag : SimpleTag
	{
		public ZoneItemTag() : base(true, "zoneitem", 0) { }

		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			return String.Format(CultureInfo.InvariantCulture, "<li>{0}", content);
		}
	}

	[CLSCompliant(false)]
	public class PageKeywords : SimpleTag
	{
		public PageKeywords() : base(false, "PageKeyWords", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			string keywords = string.Empty;
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			ItemInstance i = a.Item;
			foreach (ItemProperty prop in i.Properties.Where(p => p.KeyName == "Keywords"))
			{
				keywords += string.Concat(prop.KeyValue, ",");
			}
			return keywords.TrimEnd(',');
		}
	}


	[CLSCompliant(false)]
	public class PageDescription : SimpleTag
	{
		public PageDescription() : base(false, "PageDescription", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			string keywords = string.Empty;
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			ItemInstance i = a.Item;
			foreach (ItemProperty prop in i.Properties.Where(p => p.KeyName == "Description"))
			{
				keywords += string.Concat(prop.KeyValue, ",");
			}
			return keywords.TrimEnd(',');
		}
	}

	[CLSCompliant(false)]
	public class PagePublisher : SimpleTag
	{
		public PagePublisher() : base(false, "PagePublisher", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			string keywords = string.Empty;
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			ItemInstance i = a.Item;
			foreach (ItemProperty prop in i.Properties.Where(p => p.KeyName == "Publisher"))
			{
				keywords += string.Concat(prop.KeyValue, ",");
			}
			return keywords.TrimEnd(',');
		}
	}

	[CLSCompliant(false)]
	public class HasRightColumn : SimpleTag
	{
		public HasRightColumn() : base(false, "HasRightColumn", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			
			var result = a.Item.Relationships.Where(p => string.Equals(p.RelationshipType, "RightColumn")).Any();
			return result.ToString();
		}
	}

	[CLSCompliant(false)]
	public class HasLeftColumn : SimpleTag
	{
		public HasLeftColumn() : base(false, "HasLeftColumn", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			
			var result = a.Item.Relationships.Where(p => string.Equals(p.RelationshipType, "LeftColumn")).Any();
			return result.ToString();
		}
	}


	[CLSCompliant(false)]
	public class PageStyle : SimpleTag
	{
		public PageStyle() : base(false, "PageStyle", 0) { }
		public override string ProcessTag(IContext context, string content, object[] parms)
		{
			var o = context.tryfind("Page");
			ItemTemplate.TemplateItem a = (ItemTemplate.TemplateItem)o.Value;
			ItemInstance i = a.Item;

			if (a.Item.Relationships.Where(p => string.Equals(p.RelationshipType, "RightColumn")).Count() > 0)
			{
				return @"<style type=""text/css"">
						#page {margin-top:2px;float:left;border-left:210px solid #FFF;border-right:210px solid #FFF;clear:both;width:480px;}
						#content {float:left;width:460px;margin-right:-480px;margin-top:36px;border-left:0px solid #FFF;margin-left:0px;padding:0px;padding-right:0px;}
						#content h2 {margin-top:0px;}
						#block1 {float:left;width:210px;margin-left:-210px;position:relative;margin-top:24px;}
						#block2 {float:right;width:200px;margin-right:-200px;margin-left:-0px;position:relative;margin-top:20px;}
					</style>";
			}
			else
			{
				return @"<style type=""text/css"">
						#page {margin-top:2px;float:left;border-left:210px solid #FFF;border-right:0px solid #FFF;clear:both;width:690px;}
						#content {float:left;width:640px;margin-right:-690px;margin-top:36px;border-left:0px solid #FFF;margin-left:0px;padding:0px;padding-right:50px;}
						#content h2 {margin-top:0px;}
						#block1 {float:left;width:210px;margin-left:-210px;position:relative;margin-top:24px;}
						#block2 {float:right;width:100px;margin-right:-0px;margin-left:-100px;position:relative;margin-top:20px;}
					</style>";
			}
		}
	}
}
